﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Runtime.Serialization.Json;

namespace EGov
{
    public partial class chitiettintuc2 : PhoneApplicationPage
    {
        public string PlaceImage { get; set; }
        string id = frmtinmoi.idtin;
        public chitiettintuc2()
        {
            InitializeComponent();
            //string id =(NavigationContext.QueryString["selectedItem"]);            
            string uri = "http://egov047.juddy.vn/api/place/getbyId?id=" + id;
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenReadAsync(new Uri(uri), UriKind.Absolute);
            string uricomment = "http://egov047.juddy.vn/api/comment/GetByPlaceId?placeId=" + id;         
            WebClient commentclient = new WebClient();            
            commentclient.OpenReadCompleted += new OpenReadCompletedEventHandler(commentclient_OpenReadCompleted);
            commentclient.OpenReadAsync(new Uri(uricomment), UriKind.Absolute);
        }

        private void commentclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            List< Comment> listcomment= new List<Comment>();
            var ser = new DataContractJsonSerializer(typeof(List<Comment>), null);
            listcomment = (List<Comment>)ser.ReadObject(e.Result);
            lstListcomment.ItemsSource = listcomment;
        }

        private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            Place place = new Place();
            List<string>stringimage= new List<string>();
            List<anh> listanh = new List<anh>();
            var ser = new DataContractJsonSerializer(typeof(Place), null);
             place = (Place)ser.ReadObject(e.Result);            
            if (place.Title == null)
                place.Title = "no found";
            if (place.Summary == null)
                place.Summary = "no found";           
            if (place.Description == null)
                place.Description = "no found";
            if (place.Priority == null)
                place.Priority = "no found";
            if (place.Longtitude == null)
                place.Longtitude = "no found";
            if (place.Address == null)
                place.Address = "no found";
            if (place.Image == null)
                place.Image = "http://egov047.juddy.vn/_serviceData/Images/77.jpg";
            stringimage=place.PlaceImages;
            this.tbxTilte.Text = place.Title;
            this.tbxSummary.Text = place.Summary;
            this.tbxPriority.Text = place.Priority;
            this.tbxLonger.Text = place.Longtitude;
            this.tbxDesription.Text = place.Description;           
            this.tbxAddress.Text = place.Address;          
            this.datePickerCreated.ValueStringFormat = place.DateCreated;
            for(int i=0;i<stringimage.Count;i++)
            {
                listanh.Add(new anh{id=i,uri=stringimage[i]});
            }
            MyPanoram.Items.Clear();
            for(int i=0;i<listanh.Count;i++)
            {
               
                List<anh> temp = new List<anh>();
                temp.Add(listanh[i]);
                ListBox lb = new ListBox();
                lb.ItemTemplate = lstListImage.ItemTemplate;
                lb.ItemsSource = temp;
                PanoramaItem pi = new PanoramaItem();
                //pi.Name = "_PanoramaItem" + i;                   
                pi.Content = lb;
                MyPanoram.Items.Add(pi);
            }
        }
        private void btnComment_Click(object sender, RoutedEventArgs e)
        {
            string content = this.tbxcommment.Text;
            string inserturicomment = "http://egov047.juddy.vn/api/comment/Insert?usertId=1&content="+content+"&placeId=" + id;
            WebClient insertcommentclient = new WebClient();
            insertcommentclient.OpenReadCompleted += new OpenReadCompletedEventHandler(insertcommentclient_OpenReadCompleted);
            insertcommentclient.OpenReadAsync(new Uri(inserturicomment), UriKind.Absolute);
        }

        private void insertcommentclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            var ser = new DataContractJsonSerializer(typeof(string), null);
            string ac = (string)ser.ReadObject(e.Result);
            MessageBox.Show(ac);
        }

       
    }
    public class anh
    {
        public int id {get;set;}
        public string uri { get; set; }
    }
}